from django.db.models.query import QuerySet
from django.views.generic import FormView, CreateView, UpdateView, DeleteView,ListView,DetailView
from django.urls import reverse_lazy
from .models import PersonInfo
from .forms import PersonInfoForm
from django.views.generic.dates import MonthArchiveView,WeekArchiveView
from django.urls import path,include
from django.shortcuts import render
from django.http import HttpResponse


class PersonInfoMonthArchiveView(MonthArchiveView):
    allow_empty=True
    allow_future=True
    context_object_name='mylist'
    success_url='/result'
    model=PersonInfo
    date_field='hire_date'
    queryset=PersonInfo.objects.all()
    year_format='%Y'
    month_format='%m'
    paginate_by=50
    model = PersonInfo
    template_name = 'person_info_month_archive.html'
    date_field = 'hire_date'

class PersonInfoWeekArchiveView(WeekArchiveView):
    allow_empty = True
    allow_future = True
    context_object_name = 'mylist'
    template_name = 'person_info_week_archive.html'
    model = PersonInfo
    date_field = 'hire_date'
    queryset = PersonInfo.objects.all()
    year_format = '%Y'
    week_format = '%W'
    paginate_by = 50

class PersonInfoFormView(FormView):
    template_name = 'person_info_form.html'
    form_class = PersonInfoForm
    success_url = '/result'
    form_class=PersonInfoForm
    initial={'name':'lyx','age':30,'hire_date':'2024-10-13'}
    extra_context={'title':'PersonInfo'}

class PersonInfoCreateView(CreateView):
    template_name = 'person_info_form.html'
    success_url = '/result'
    initial={'name':'lyx','age':30,'hire_date':'2024-10-13'}
    model=PersonInfo
    fields=['name','age','hire_date']
    extra_context={'title':'PersonInfo'}

class PersonInfoUpdateView(UpdateView):
    model = PersonInfo
    template_name = 'person_info_update.html'
    success_url = '/result'
    fields=['name','age','hire_date']
    slug_url_kwarg='pk'
    slug_field='pk'
    context_object_name='PersonInfo'
    extra_context={'title':'PersonInfo'}

class PersonInfoDeleteView(DeleteView):
    model = PersonInfo
    template_name = 'person_info_delete.html'
    success_url = '/result'
    context_object_name='PersonInfo'
    extra_context={'title':'PersonInfo'}

class PersonInfoListView(ListView):
    model=PersonInfo
    template_name='person_info_list.html'
    context_object_name='person_list'
    def get_queryset(self):
        queryset=super().get_queryset()
        search_query=self.request.GET.get('search')
        if search_query:
            queryset=queryset.filter(name_icontains=search_query)
        return queryset

class PersonInfoDetailView(DetailView):
    model=PersonInfo
    template_name='person_info_detail.html'
    extra_context={'title':'PersonInfo'}
    slug_field='age'
    slug_url_kwarg='age'
    pk_url_kwarg='pk'

def result(request):
    return HttpResponse('Success')

def myapp(request):
    return render(request,'myapp.html')
# Create your views here.

